#include "flash.h"

/**
 * @note 读不需要进行解锁
 */
uint32_t FLASH_ReadWord(uint32_t addr)
{
    return *((__IO uint32_t *)addr); // 4bytes
}

/**
 * @note 读不需要进行解锁
 */
uint16_t FLASH_ReadHalfWord(uint32_t addr)
{
    return *((__IO uint16_t *)addr); // 2bytes
}

/**
 * @note 读不需要进行解锁
 */
uint8_t FLASH_ReadByte(uint32_t addr)
{
    return *((__IO uint8_t *)addr); // 1byte
}

void FLASH_WriteHalfWord(uint32_t addr, uint16_t dat)
{
    FLASH_Unlock(); //
    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
    FLASH_ErasePage(addr);            // 擦除页
    FLASH_ProgramHalfWord(addr, dat); // [半字]2byte
    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
    FLASH_Lock();
}
